package com.example.traccarmanager.slice;

import com.example.traccarmanager.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.TextField;
import ohos.agp.components.webengine.*;
import ohos.agp.window.dialog.ToastDialog;
import ohos.event.commonevent.*;
import ohos.rpc.RemoteException;
import ohos.utils.net.Uri;
import static ohos.aafwk.content.Intent.*;

public class MainFragmentSlice extends AbilitySlice {
    private BrowserAgent browserAgent = new BrowserAgent(getContext());
    private WebView webView;
    private TextField textField;
    private MyStub sub;
    private static String keyToken = "eventLogin";
    private static final String EVENT_LOGIN = "eventLogin";
    private static final String EVENT_TOKEN = "eventToken";
    /**
     *
     */
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_activity_main);
        webView = (WebView) findComponentById(ResourceTable.Id_webview);
        String url = intent.getStringParam("urls");
        WebConfig webConfig = webView.getWebConfig();
        webConfig.setDataAbilityPermit(true);
        webConfig.setJavaScriptPermit(true);
        webConfig.setLoadsImagesPermit(true);
        webConfig.setLocationPermit(true);
        webConfig.setSecurityMode(WebConfig.SECURITY_SELF_ADAPTIVE);
        webConfig.setMediaAutoReplay(true);
        webView.setBrowserAgent(new BrowserAgent(getContext()) {
            @Override
            public boolean onPickFiles(WebView webView1, AsyncCallback<Uri[]> urisCallback, PickFilesParams params) {
                ToastDialog toastDialog = new ToastDialog(getContext());
                toastDialog.setText("Pull up");
                toastDialog.show();
//              Todo 不支持直接打开系统文件
                String[] permissions = {"ohos.permission.READ_USER_STORAGE"};
                requestPermissionsFromUser(permissions, 0);
                    Intent intent = new Intent();
                    Operation operation = new Intent.OperationBuilder().
                            withAction(Intent.ACTION_PLAY)
                            .build();
                    intent.addFlags(Intent.FLAG_NOT_OHOS_COMPONENT);
                    intent.setType("image/*");
                    intent.setOperation(operation);
                    startAbilityForResult(intent, 1);
                return super.onPickFiles(webView, urisCallback, params);
            }
        });

        webView.load(url);
        webView.executeJs("javascript:callFuncInWeb()", new AsyncCallback<String>() {
            @Override
            public void onReceive(String msg) {
                try {
                        Intent intent = new Intent();
                        Operation operation = new OperationBuilder()
                        .withAction(EVENT_LOGIN)
                            .build();
                        intent.setOperation(operation);
                        CommonEventData eventData = new CommonEventData(intent);CommonEventManager.publishCommonEvent(eventData);
                    } catch (RemoteException e) {
                    }
            }
        });
        MatchingSkills matchingSkills = new MatchingSkills();
        matchingSkills.addEvent(EVENT_TOKEN);
        CommonEventSubscribeInfo subscribeInfo = new CommonEventSubscribeInfo(matchingSkills);
        sub = new MyStub(subscribeInfo);
        try {
            CommonEventManager.subscribeCommonEvent(sub);
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }
    /**
     *
     */
    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    /**
     *
     */
    @Override
    public void onActive() {
        super.onActive();
    }

    public static class MyStub extends CommonEventSubscriber {
        private WebView webView;

        public MyStub(CommonEventSubscribeInfo subscribeInfo) {
            super(subscribeInfo);
        }

        /**
         *
         */
        @Override
        public void onReceiveEvent(CommonEventData commonEventData) {
            commonEventData.setData(keyToken);
            String token = commonEventData.getData();
            String code = "updateNotificationToken && updateNotificationToken('" + token + "')";
            webView.executeJs(code, null);
        }
    }
}
